package com.huixuebao.after.controller.groupBuy;


import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.huixuebao.after.dto.PolicyGroupDTO;
import com.huixuebao.after.dto.SubmitPolicyGroupDTO;
import com.huixuebao.after.service.PolicyOrderService;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

@Slf4j
@RestController
@RequestMapping("/hxb/after/group")
public class GroupBuyController {

    @Autowired
    PolicyOrderService policyOrderService;
    /**
     * 导入机构信息
     */
    @RequestMapping(value = "/groupBuyExcel", method = RequestMethod.POST)
    public ApiResult groupBuyExcel(@RequestParam(value = "file", required = false) MultipartFile file) {
        ApiResult apiResult = new ApiResult();
        try {
            ImportParams importParams = new ImportParams();
            importParams.setHeadRows(1);
            InputStream inputStream = new ByteArrayInputStream(file.getBytes());
            ExcelImportResult<PolicyGroupDTO> result = ExcelImportUtil.importExcelMore(inputStream, PolicyGroupDTO.class, importParams);

            //返回前端list列表
            //policyOrderService.postBackData(result.getList());
            apiResult.setData(result.getList());
            apiResult.setSuccess(true);
            apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
            apiResult.setMessage("导入信息成功");
            return  apiResult;
        } catch (Exception e) {
            log.error("import file failure", e);
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("导入信息失败");
            return apiResult;
        }
    }

    /**
     * 前端数据确认没问题后提交数据。
     * @param submitPolicyGroupDTO
     * @return
     */
    @PostMapping("/submitGroupPolicy")
    public ApiResult submitGroupPolicy(@RequestBody SubmitPolicyGroupDTO submitPolicyGroupDTO){

        return policyOrderService.submitGroupPolicy(submitPolicyGroupDTO);
    }
}
